﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Windows.Forms;
using ArcEngine.Graphics;
using ArcEngine.Resources;
using WeifenLuo.WinFormsUI.Docking;


namespace ArcEngine.Editor
{
	public partial class ResourceForm : DockContent
	{
		/// <summary>
		/// 
		/// </summary>
		public ResourceForm()
		{
			InitializeComponent();
		}



		/// <summary>
		/// Rebuilds the resource TreeView
		/// </summary>
		public void RebuildResourceTree()
		{
			TreeNode node;
			TreeNode root;
			TreeNode bank;
			List<string> list;

			RessourceTree.Nodes.Clear();
			bank = RessourceTree.Nodes.Add("Bank");



			// Animation
			list = ResourceManager.GetAnimations();
			root = bank.Nodes.Add("Animations");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetAnimation(name);
				node.ImageIndex = 28;
				node.SelectedImageIndex = 28;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Animations (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Animation);




			// TileSets
			root = bank.Nodes.Add("TileSets");
			list = ResourceManager.GetTileSets();
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetTileSet(name);
				node.ImageIndex = 28;
				node.SelectedImageIndex = 28;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "TileSets (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(TileSet);

			// Layout
			root = bank.Nodes.Add("Layout");
			list = ResourceManager.GetLayouts();
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetLayout(name);
				node.ImageIndex = 28;
				node.SelectedImageIndex = 28;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Layouts (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Layout);


			// Levels
			list = ResourceManager.GetLevels();
			root = bank.Nodes.Add("Levels");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetLevel(name);
				node.ImageIndex = 6;
				node.SelectedImageIndex = 6;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Levels (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Level);


			// Models
			list = ResourceManager.GetModels();
			root = bank.Nodes.Add("Models");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetModel(name);
				node.ImageIndex = 25;
				node.SelectedImageIndex = 25;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Models (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Model);


			// Script
			root = bank.Nodes.Add("Scripts");
			list = ResourceManager.GetScripts();
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetScript(name);
				node.ImageIndex = 24;
				node.SelectedImageIndex = 24;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Scripts (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Script);


			// Texture
			list = ResourceManager.GetTextures();
			root = bank.Nodes.Add("Textures");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetTexture(name);
				node.ImageIndex = 27;
				node.SelectedImageIndex = 27;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Textures (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Texture);


			// Font
			list = ResourceManager.GetTTFFonts();
			root = bank.Nodes.Add("Texture Fonts");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetTTFFont(name);
				node.ImageIndex = 4;
				node.SelectedImageIndex = 4;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Fonts (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(TTFFont);



			// String tables
			list = ResourceManager.GetStringTables();
			root = bank.Nodes.Add("StringsTables");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetStringTable(name);
				node.ImageIndex = 28;
				node.SelectedImageIndex = 28;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "StringsTables (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(StringTable);


			// Binaries
			list = ResourceManager.GetBinaries();
			root = bank.Nodes.Add("Binaries");
			foreach (string name in list)
			{
				node = new TreeNode(name);
				node.Tag = ResourceManager.GetBinary(name);
				node.ImageIndex = 33;
				node.SelectedImageIndex = 33;
				node.ContextMenuStrip = BankContextMenu;
				root.Nodes.Add(node);
			}
			root.Text = "Binaries (" + root.Nodes.Count + ")";
			root.ImageIndex = 34;
			root.SelectedImageIndex = 35;
			root.Tag = typeof(Binary);



			// Trie le tout
			RessourceTree.Sort();
			bank.Expand();

		}


		/// <summary>
		/// Collapse resource tree
		/// </summary>
		public void CollapseTree()
		{
			foreach (TreeNode node in RessourceTree.Nodes[0].Nodes)
			{
				node.Collapse();
			}
		}

		/// <summary>
		/// Expand resource tree 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void ExpandTree()
		{
			RessourceTree.ExpandAll();
		}




		#region Events

		/// <summary>
		/// OnMouseUp ContextMenu
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnMouseUp(object sender, MouseEventArgs e)
		{

			if (e.Button == MouseButtons.Right)
			{
				TreeNode node = RessourceTree.GetNodeAt(e.X, e.Y);
				BankContextMenu.Tag = node.Tag;

				BankContextMenu.Show(RessourceTree, new Point(e.X, e.Y));
			}
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void EraseMenu_Click(object sender, EventArgs e)
		{
			// Erase a specific resource
			if (BankContextMenu.Tag is ResourceBase)
			{
				ResourceBase res = BankContextMenu.Tag as ResourceBase;

				if (MessageBox.Show("Do you really want to delete " + res.GetType().Name + " \"" + res.Name + "\" ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
					return;

				if (BankContextMenu.Tag is Binary)
				{
					ResourceManager.DestroyBinary(res.Name);
				}
				else if (BankContextMenu.Tag is Level)
				{
					ResourceManager.DestroyLevel(res.Name);
				}
				else if (BankContextMenu.Tag is Model)
				{
					ResourceManager.DestroyModel(res.Name);
				}
				else if (BankContextMenu.Tag is Script)
				{
					ResourceManager.DestroyScript(res.Name);
				}
				else if (BankContextMenu.Tag is StringTable)
				{
					ResourceManager.DestroyStringTable(res.Name);
				}
				else if (BankContextMenu.Tag is Texture)
				{
					ResourceManager.DestroyTexture(res.Name);
				}
				else if (BankContextMenu.Tag is TTFFont)
				{
					ResourceManager.DestroyTTFFont(res.Name);
				}
				else if (BankContextMenu.Tag is TileSet)
				{
					ResourceManager.DestroyTileSet(res.Name);
				}
			}

			// Erase all resource of this kind
			else
			{

			}





			RebuildResourceTree();
		}


		/// <summary>
		/// When double click on an element in the treeview then open a new window
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnTreeViewDoubleCick(object sender, EventArgs e)
		{
			TreeView view = (TreeView)sender;

			// Not an editable node
			if (view.SelectedNode == null || view.SelectedNode.Tag == null)
				return;




			if (view.SelectedNode.Tag is StringTable)
			{
				StringTableForm form = new StringTableForm();
				form.Init((StringTable)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}

			else if (view.SelectedNode.Tag is Texture)
			{
				TextureForm form = new TextureForm();
				form.Init((Texture)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is Script)
			{
				ScriptForm fm = new ScriptForm();
				fm.Init((Script)view.SelectedNode.Tag);
				fm.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is Level)
			{
				LevelForm form = new LevelForm();
				form.Init((Level)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is TileSet)
			{
				TileSetForm form = new TileSetForm();
				form.Init((TileSet)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is TTFFont)
			{
				TTFFontForm form = new TTFFontForm();
				form.Init((TTFFont)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is Model)
			{
				ModelForm form = new ModelForm();
				form.Init((Model)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is Binary)
			{
				return;
			}
			else if (view.SelectedNode.Tag is Layout)
			{
				LayoutForm form = new LayoutForm();
				form.Init((Layout)view.SelectedNode.Tag);
				form.Show(DockPanel);
			}
			else if (view.SelectedNode.Tag is Animation)
			{
				AnimationForm fm = new AnimationForm();
				fm.Init((Animation)view.SelectedNode.Tag);
				fm.Show(DockPanel);
				
			}
			else
			{
				Log.Send(new LogEventArgs(LogLevel.Info, "\"" + view.SelectedNode.Tag.ToString() + "\" not yet editable...\n", null));
				return;
			}

		}

		#endregion

	}
}
